import {
	version
} from "@/common/config.js"
import {
	postCode
} from "@/api/login"
import store from "@/store/index.js"
import {
	_wx
} from "@/common/utils.js"

// #ifdef MP-WEIXIN
uni.login({
	success: res => {
		let code = res.code
		postCode({
			code: code
		}).then(res => {
			store.commit("setToken", res.data.token)
			store.commit("setOpenid", res.data.openid)
			store.commit("setUnionid", res.data.unionid)
			let userinfo = res.data.userInfo
			let hasRegister
			if (JSON.stringify(userinfo) === "[]" || !userinfo) {
				hasRegister = false
			} else {
				hasRegister = true
				store.commit("setUserInfo", userinfo)
			}
			store.commit("setHasRegister", hasRegister)
		}).catch(err => {
			console.log(err)
		})
	}
})
// #endif

// #ifdef H5
if (_wx.is_wx() && !version.embedded) {
	const open_id = store.state.openid;
	const hasRegister = store.state.hasRegister;
	const token = store.state.token;
	if (!open_id || (open_id && hasRegister && !token)) {
		_wx.set_wx_info();
	}
} else {
	store.dispatch("updateUserInfo")
}
// #endif
